/*
 * shape.cpp
 *
 *  Created on: 03 May 2012
 *      Author: hframe
 */

#include "shape.h"

startnamespace( androidplus )startnamespace(ui)
startnamespace(widget)

shape::shape() {

}

shape::~shape() {

}

t_string shape::vertex_shader() {
	t_string v_shader_str=
	t_string("attribute vec4  a_color; \n")+
	t_string("attribute vec4  a_position;  \n")+
	t_string("uniform  mat4   u_projection;\n")+
	t_string("uniform  mat4   u_camera;\n")+
	t_string(" varying vec4  v_color; \n")+
	t_string("void main() \n")+
	t_string("{ \n")+
	t_string("v_color=a_color;\n")+
	t_string("gl_PointSize=1.0;\n")+
	t_string(" gl_Position =u_projection*u_camera*a_position; \n")+
	t_string("} \n");
	return v_shader_str;
}
t_string shape::fragment_shader() {

	t_string f_shader_str=
	t_string("precision highp float;\n")+
	t_string("varying vec4 v_color; \n")+
	t_string("void main() \n")+
	t_string("{ \n")+
	t_string(" gl_FragColor=v_color;\n")+
	t_string("} \n");
	return f_shader_str;
}
t_string shape::get_type_name() {
	return t_string("shape");
}
void shape::render(surface *srfc) {
	GLint program=srfc->grph->create_program(get_type_name().c_str(),vertex_shader().c_str(),fragment_shader().c_str());

	glUseProgram(program);

	glVertexAttrib4f(0,pen_color.red,pen_color.green,pen_color.blue,pen_color.alpha);

	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const void*) vertices);
	glEnableVertexAttribArray(1);

	if(!srfc->grph->is_projection_matrix_loaded) {
		t_float *projection_matrix=srfc->grph->projection_matrix-> to_float_array();
		t_float *camera_matrix=srfc->grph->camera_matrix->to_float_array();

		drawing::matrix pr(*srfc->grph->projection_matrix);
		drawing::matrix cm(*srfc->grph->camera_matrix);
		pr=cm *pr;



		LOGI("çalissana ulan\n");
		for(int i=0;i<4;++i)
		{
			LOGI("%f %f %f %f projection\n",projection_matrix[i*4+0],projection_matrix[i*4+1],projection_matrix[i*4+2],projection_matrix[i*4+3]);
		}

		for(int i=0;i<4;++i)
			{
				LOGI("%f %f %f %f camera\n",camera_matrix[i*4+0],camera_matrix[i*4+1],camera_matrix[i*4+2],camera_matrix[i*4+3]);
			}
		for(int i=0;i<4;++i)
				{
					LOGI("%f %f %f %f projection*camera\n",pr[i*4+0],pr[i*4+1],pr[i*4+2],pr[i*4+3]);
				}






		int projection_location = glGetUniformLocation(program, "u_projection");
		LOGI("u_project location %d",projection_location);
		glUniformMatrix4fv( projection_location,1,GL_FALSE, projection_matrix);

		int camera_location = glGetUniformLocation(program, "u_camera");
			LOGI("u_camera location %d",camera_location);
			glUniformMatrix4fv( camera_location,1,GL_FALSE, camera_matrix);

		srfc->grph->is_projection_matrix_loaded=1;
	}

	glDrawElements(GL_TRIANGLES,indices_n,GL_UNSIGNED_BYTE,indices);

	LOGI("Shape Finished");
}

endnamespace(widget)
endnamespace(ui)
endnamespace(androidplus)
